#
# ChangeLog
# 0.1 - 04.04.2019:
#    - return immediately if transport endpoint is not connected
#    - start of ChangeLog
VERSION=0.1

STATUS_OK=0
STATUS_WARNING=1     # Check timed out or EOS resource consumption high or
                     # previous error status detected
STATUS_CRITICAL=2    # EOS not working
STATUS_UNKNOWN=3     # Internal or usage error

RETURN_STATUS=$STATUS_OK
TIMEOUT_SECONDS=120

EOS_MOUNT_DIR="/eos/"
SUBDIR="user argo asg besfs3 lhaaso public wcda "

usage() {
   /bin/echo "Usage:   $0 "
   /bin/echo "Example: $0 "
   /bin/echo "Options:"
   /bin/echo "  -t  second after which the check times out with a warning (default: ${TIMEOUT_SECONDS})"
   /bin/echo "  -n  run extended network checks"
   /bin/echo "  -m  check memory consumption of the cvmfs2 process"
   /bin/echo "      (less than 50M or 1% of available memory)"
}

version() {
   /bin/echo "EOS automount check for Nagios, version: $VERSION"
}



function send_mail(){
        echo "Warning!!!"|mutt -s "$(hostname -s) eos automount $1" -c biyujiang@ihep.ac.cn  lihaibo@ihep.ac.cn >> /dev/null 2&1
}


# Grab mountpoint / basic availability
for sub in ${SUBDIR};do
	cd "${EOS_MOUNT_DIR}/${sub}" && ls . > /dev/null
	if [ $? -ne 0 ]; then
	        /bin/echo "SERVICE STATUS: failed to access ${EOS_MOUNT_DIR}/${sub}"
		umount -l /eos/${sub}
		umount -l /eos/${sub}
	        send_mail "SERVICE STATUS: failed to access ${EOS_MOUNT_DIR}/${sub}, has executed umount -l /eos/${sub}"
	        #exit $STATUS_CRITICAL
	fi
done

echo "SERVICE STATUS: OK"
exit $RETURN_STATUS

